package com.nanohadoop.yarn;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 表示提交到YARN的应用程序
 */
public class ApplicationMaster implements Serializable {
    private static final long serialVersionUID = 1L;

    private String applicationId;
    private String name;
    private String user;
    private Resource requiredResource;
    private int numContainers;
    private ApplicationStatus status;
    private List<Container> allocatedContainers;

    public enum ApplicationStatus {
        SUBMITTED, RUNNING, COMPLETED, FAILED, KILLED
    }

    public ApplicationMaster(String applicationId, String name, String user, Resource requiredResource, int numContainers) {
        this.applicationId = applicationId;
        this.name = name;
        this.user = user;
        this.requiredResource = requiredResource;
        this.numContainers = numContainers;
        this.status = ApplicationStatus.SUBMITTED;
        this.allocatedContainers = new ArrayList<>();
    }

    // Getters and Setters
    public String getApplicationId() { return applicationId; }
    public void setApplicationId(String applicationId) { this.applicationId = applicationId; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getUser() { return user; }
    public void setUser(String user) { this.user = user; }
    public Resource getRequiredResource() { return requiredResource; }
    public void setRequiredResource(Resource requiredResource) { this.requiredResource = requiredResource; }
    public int getNumContainers() { return numContainers; }
    public void setNumContainers(int numContainers) { this.numContainers = numContainers; }
    public ApplicationStatus getStatus() { return status; }
    public void setStatus(ApplicationStatus status) { this.status = status; }
    public List<Container> getAllocatedContainers() { return allocatedContainers; }
    public void addContainer(Container container) { this.allocatedContainers.add(container); }

    /**
     * 检查作业是否已完成
     */
    public boolean isJobCompleted() {
        return status == ApplicationStatus.COMPLETED ||
               status == ApplicationStatus.FAILED ||
               status == ApplicationStatus.KILLED;
    }

    @Override
    public String toString() {
        return "Application{" +
                "applicationId='" + applicationId + '\'' +
                ", name='" + name + '\'' +
                ", user='" + user + '\'' +
                ", requiredResource=" + requiredResource +
                ", numContainers=" + numContainers +
                ", status=" + status +
                ", allocatedContainers=" + allocatedContainers.size() +
                '}';
    }
}